<?php
namespace application\datatypes;
use application\utilities\Utility;

class PhoneNumber extends DataType {
	public static $name = 'Phone Number';
	public static $fields = array(array('columnType'=>'VARCHAR(20)'));
	
	public static function validate($field, $data, $results) {
		$pattern = "/[^0-9\-\(\)ex\s.]/";
		if($data[$field->cf_column]) {
			if(preg_match($pattern, $data[$field->cf_column])) {
				$results['errors'][] = $field->cf_display_name.' must be a valid phone number.';
			}
			else {
				$results['db_fields'][$field->cf_column] = $data[$field->cf_column];
			}
		}
		elseif($field->cf_required) {
			$results['errors'][] = $field->cf_display_name.' is required.';
		}		

		return $results;
	}
	
	public static function display($formElements) {
		?>
		
		<fieldset class="single-field">
			<header><?=$formElements['display']?></header>
			<section class="inner">
			<?php if($description = $formElements['description']) : ?>
				<p class="form-description"><?=$description?></p>
			<?php endif;?>
			<?=$formElements['elements']['element']?>
			</section>
		</fieldset>
		<?php
	}
	
	public static function build($field, $data) {
		$value = $data[$field->cf_column];
		$formElements = array('description'=>$field->cf_description, 'column'=>$field->cf_column, 'display'=>$field->cf_display_name, 'data_type'=>$field->cf_data_type, 'elements'=>array(
				'element'=>
					sprintf('<input type="text" value="%1$s" class="long-text phone-mask" name="%2$s" id="%2$s"/>', $value, $field->cf_column)
				
				));
		return $formElements;
	
	}
	
	public static function prepareData($field, $data) {
		return $data[$field->cf_column];
		
		
		
		
		
	}
	
	
	
}
?>